package com.zys.el.search.client;

import com.zys.el.user.api.NoteApi;
import com.zys.el.user.po.Note;
import com.zys.el.user.po.User;
import feign.hystrix.FallbackFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;


@FeignClient(name = "user-service", /*fallback = NoteClient.NoteClientFallback.class*/fallbackFactory = NoteClient.NoteClientFallbackFactory.class)
public interface NoteClient extends NoteApi {

    @Component
    @RequestMapping("/note-fallback")
    class NoteClientFallback implements NoteClient {
        private static final Logger LOGGER = LoggerFactory.getLogger(NoteClientFallback.class);

        @Override
        public List<Note> selectAll() {
            LOGGER.error("异常出现，进入fallback方法");
            return null;
        }
    }

    @Component
    @RequestMapping("/note-fallback-factory")
    class NoteClientFallbackFactory implements FallbackFactory<NoteClient> {


        Logger logger = LoggerFactory.getLogger(NoteClient.NoteClientFallbackFactory.class);
        @Override
        public NoteClient create(Throwable throwable) {
            throwable.printStackTrace();
            logger.error(throwable.getMessage());
            return new NoteClient() {

                @Override
                public List<Note> selectAll() {
                    return null;
                }
            };
        }
    }
}
